import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ClassSerializerInterceptor,
  UseInterceptors,
  Query,
  UseGuards,
} from '@nestjs/common';
import { CourseService } from './course.service';
import { CreateCourseDto } from './dto/create-course.dto';
import { UpdateCourseDto } from './dto/update-course.dto';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { GetCourseDto, ReturnCourseDtoList } from './dto/get-course-dto';
import { JwtGuard } from '../guards/jwt.guards';

@Controller('course')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(JwtGuard) // 校验token是否通过
@ApiTags('课程模块')
export class CourseController {
  constructor(private readonly courseService: CourseService) {}

  @Post()
  @ApiOperation({ summary: '创建课程' })
  @ApiBody({ type: CreateCourseDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateCourseDto,
  })
  create(@Body() createCourseDto: CreateCourseDto) {
    return this.courseService.create(createCourseDto);
  }

  @Get()
  @ApiOperation({ summary: '课程列表' })
  @ApiBody({ type: GetCourseDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: ReturnCourseDtoList,
  })
  findAll(@Query() query: GetCourseDto) {
    return this.courseService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '课程详情' })
  @ApiQuery({ name: 'id', required: true, description: '课程id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateCourseDto,
  })
  findOne(@Param('id') id: number) {
    return this.courseService.findOne(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '课程更新' })
  @ApiQuery({ name: 'id', required: true, description: '课程id' })
  @ApiBody({ type: UpdateCourseDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateCourseDto,
  })
  update(@Param('id') id: number, @Body() updateCourseDto: UpdateCourseDto) {
    return this.courseService.update(id, updateCourseDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '课程删除' })
  @ApiQuery({
    name: 'id',
    required: true,
    description: '',
  })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateCourseDto,
  })
  remove(@Param('id') id: number) {
    return this.courseService.remove(id);
  }
}
